package org.fuckerfucker.todoapp_kotlin.fragments

import android.app.Application
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import org.fuckerfucker.todoapp_kotlin.data.model.Todo
import org.fuckerfucker.todoapp_kotlin.R
import org.fuckerfucker.todoapp_kotlin.data.model.Priority


class SharedViewModel(application: Application): AndroidViewModel(application) {
    // =================== List fragment
    val emptyDatabase: MutableLiveData<Boolean> = MutableLiveData(false)

    fun checkIfDatabaseEmpty(todo: List<Todo>) {
        emptyDatabase.value = todo.isEmpty()
    }

    // =================== Add/Update fragment
    val listner = object : AdapterView.OnItemSelectedListener{
        override fun onItemSelected(
            parent: AdapterView<*>?,
            view: View?,
            position: Int,
            id: Long
        ) {
            when(position) {
                0 -> { (parent?.getChildAt(0) as TextView).setTextColor(ContextCompat.getColor(application, R.color.red))}
                1 -> {(parent?.getChildAt(0) as TextView).setTextColor(ContextCompat.getColor(application, R.color.yellow))}
                2 -> {(parent?.getChildAt(0) as TextView).setTextColor(ContextCompat.getColor(application, R.color.green))}
            }
        }

        override fun onNothingSelected(parent: AdapterView<*>?) {
            Log.i("SharedViewModel.kt", "you select nothing")
        }
    }

    // check input data ok? title and description is required
    fun verifyDataFromUser(title: String, desc: String): Boolean {
        return !(title.isEmpty() || desc.isEmpty())
    }

    fun parsePriority(priority: String): Priority {
        return when(priority) {
            "High Priority" -> {
                Priority.HIGH
            }
            "Medium Priority" -> {
                Priority.MEDIUM
            }
            "Low Priority" -> {
                Priority.LOW
            }
            // by default, using low level
            else -> Priority.LOW
        }
    }

}